<!--这是专栏详情-->
<template>
  <div class="margin-t margin-r margin-l" v-loading="this.listLoading">
    <div class="bg-f5f7fa padding20 c-overflow-h">
      <div>
        <img
          :src="columnData.cover?columnData.cover:'https://knowledge-payment.oss-cn-beijing.aliyuncs.com/column/2018-06-15/DU3mDplst6bhocbCANi1DKtMcrUQ8oVp8Oj2bwBi.png'"
          class="fl" width="180px;" height="120px">
        <div class="fl margin-l">
          <div class="fl">
            <p class="margin-b12 font-s16 color-222">{{columnData.name}}</p>
            <p class="margin-b12 font-s14 color-ff3b3b">￥{{columnData.price}}</p>
            <p class="margin-b12 font-s12 color-999"><span
              class="margin-r30">实际订阅量 {{columnData.subscribers}}</span><span>单品点击量 {{columnData.clicks}}</span></p>
            <p class="color-222" v-if="permission == 3">
              <el-button v-if="columnData.isUpdating" @click="handle(3)">停止更新</el-button>
              <el-button v-else @click="handle(4)">开启更新</el-button>
              <el-button v-if="columnData.enableShowCourses" @click="handle(5)">关闭内容展示</el-button>
              <el-button v-else @click="handle(6)">开启内容展示</el-button>
            </p>
          </div>
        </div>
        <div v-if="permission == 3" class="textRight fr font-s14">
          <p class="c-mh16 margin-b10 font-s14">
            <a href="javascript:" @click="linkToEdit">编辑</a><span class="color-409">|</span>
            <a v-if="columnData.status" @click="handle(1)">下架</a>
            <a v-else @click="handle(2)">上架</a><span class="color-409">|</span>
            <a @click="handCopy(columnData.columnUrl, $event)">复制链接</a>
          </p>
          <p class="color-5c6 margin-t60" v-if="columnData.isUpdating">更新至第{{columnData.hasCourse}}期&nbsp;(更新中)</p>
          <p class="color-5c6 margin-t60" v-else>已完结(共{{columnData.hasCourse}}期)</p>
          <!--<p class="c-mh16" v-if="columnData.status">已上架</p>-->
          <!--<p class="c-mh16" v-else>已下架</p>-->
        </div>
      </div>
    </div>
    <div class="c-pv20 margin-l margin-r50 c-overflow-h">
      <div v-if="permission == 3" class="fl">
        <el-button type="primary" @click="isShowAdd=!isShowAdd">新添课程</el-button>
        <el-button type="primary" plain @click="addCourses">添加已有课程</el-button>
      </div>
      <div class="fr">
        <el-select v-model="listQuery.selectValue" clearable placeholder="类型" class="width100">
          <!-- <el-option label="请选择"></el-option> -->
          <el-option value="1" label="视频"></el-option>
          <el-option value="2" label="图文"></el-option>
          <el-option value="3" label="音频"></el-option>
        </el-select>
        <el-input clearable v-model="listQuery.searchName" placeholder="输入名称" @keyup.enter.native="toSerach"
                  class="width250"></el-input>
        <el-button type="primary" class="search" icon="el-icon-search" @click="toSerach">搜索</el-button>
      </div>
    </div>
    <p v-show="isShowAdd" class="margin-b padding-l">
      <el-button type="warning" @click="linkToCreatecourse('videos')">视频</el-button>
      <el-button type="primary" @click="linkToCreatecourse('audios')">音频</el-button>
      <el-button type="success" @click="linkToCreatecourse('articles')">图文</el-button>
    </p>
    <el-table :data="tableCourseData" row-key='id' fit highlight-current-row
              :header-row-style="{'font-family': 'PingFangSC-Semibold','backgroundColor': '#f5f7fa','color':'#5c6065'}"
              class="c-width100 color-222 family-PingFangSC-Regular font-s12">
      <el-table-column label="名称">
        <template slot-scope="scope">
          <div class="clearfix">
            <div class="fl margin-r width100 height50" style="position:relative;">
              <img :src="scope.row.courseAvatar" width="102" height="68" @error="setDefultImg"/>
            </div>
            <div class="fl">
              <p class="line-limit-length-240 fl">{{scope.row.name}} </p>
              <el-popover class="fl font-s12" style="font-size: 12px" v-if="scope.row.name && scope.row.name.length>20"
                placement="left"
                width="400"
                trigger="hover">
                <div>{{scope.row.name}}</div>
                <i slot="reference" class="el-icon-view c-fc-deepskyblue margin-l10"></i>
              </el-popover>
              <p>上架时间：{{scope.row.createdAt}} </p>
              <p>
                <span>访问量: {{scope.row.plays}}</span>
                <span class="c-mh5">|</span>
                <span class="c-fc-blue margin-r5">￥{{scope.row.price}}</span>
                <span class="c-fc-blue type">{{scope.row.permission == 19 ? '免费' : (scope.row.permission == 18 ? '收费' : (scope.row.permission == 17 ? 'vip免费' : 'svip免费'))}}</span>
              </p>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column width="100" align="center" label="课程类型">
        <template slot-scope="scope">
          <p class="c-textAlign-c" v-if="scope.row.courseType == 0">视频</p>
          <p class="c-textAlign-c" v-if="scope.row.courseType == 1">音频</p>
          <p class="c-textAlign-c" v-if="scope.row.courseType == 2">图文</p>
        </template>
      </el-table-column>
      <el-table-column width="100" align="center" label="前台排序">
        <template slot-scope="scope">
          <p class="c-textAlign-c">
            <span @click="editSort(scope.$index)" v-show="!scope.row.isEdit"
                  class="tdedit">{{scope.row.sortIncolumn}}</span>
            <input @blur="saveSort(scope.$index,scope.row.courseId,scope.row.sortIncolumn)" v-show="scope.row.isEdit"
                   v-focus="scope.row.isEdit" class="editInput" min=0 type="number" name="" id=""
                   v-model.number="scope.row.sortIncolumn"/>
          </p>
        </template>
      </el-table-column>
      <el-table-column width="100" align="center" label="是否上架">
        <template slot-scope="scope">
          <p><span v-if="scope.row.status== 1 ">是</span>
            <span v-else>否</span></p>
        </template>
      </el-table-column>
      <el-table-column width="150" align="center" label="是否已上传(视频/音频)">
        <template slot-scope="scope" v-if="scope.row.courseType < 2">
          <p><span v-if="scope.row.hasUpload == 1 ">是</span>
            <span v-else>否</span></p>
        </template>
      </el-table-column>
      <el-table-column align="center" width="150" label="操作" v-if="permission == 3">
        <template slot-scope="scope">
          <p class="text-alignL">
            <b v-if="scope.row.courseType==0" @click="linkToEditCourse('videos',scope.row.courseId)">编辑</b>
            <b v-if="scope.row.courseType==1" @click="linkToEditCourse('audios',scope.row.courseId)">编辑</b>
            <b v-if="scope.row.courseType==2" @click="linkToEditCourse('articles',scope.row.courseId)">编辑</b><span
            class="color-409">|</span>

            <b @click="openFree(scope.row.courseId,1)" v-if="scope.row.isFreeInColumn== 1">关闭免费</b>
            <b @click="openFree(scope.row.courseId,2)" v-else>开启免费</b>

          </p>
          <p class="text-alignL">
            <b @click="openFree(scope.row.courseId,3)">移除</b><span
            v-if="scope.row.courseType==0||scope.row.courseType==1" class="color-409">|</span>

            <b v-if="scope.row.courseType==0" @click="linkToDetail(scope.row.courseId,1)">上传视频</b>
            <b v-if="scope.row.courseType==1" @click="linkToDetail(scope.row.courseId,2)">上传音频</b>
          </p>
          <div class="text-alignL"><b @click="messagePush(scope.$index)">微信推送</b></div>
        </template>
      </el-table-column>
    </el-table>
    <div class="block text-c">
      <!--分页组件 current-page:当前页   -->
      <!--   page-sizes:每页几条数据（选项）   -->
      <!--   page-size:每页几条数据   -->
      <!--   total:总条数   -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.currentPage"
        :page-sizes="[10, 20, 30, 40, 50]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="listQuery.total">
      </el-pagination>
    </div>
    <add-exit-course v-show="isShowExitCourses" @hideAddCourses="hideAddCourses"
                     :columnId="this.listQuery.columnId"></add-exit-course>
    <wechat-push v-if="isShowWechatPush" :courseData="courseData" @hideWechatPush="hideWechatPush"></wechat-push>

  </div>
</template>

<script>
  import defultImg from '@/assets/defult270.png'
  import { toGetColumnInfo, toHandle, togetCourseList, toOpenFree, toSaveSort } from '@/api/column'
  import clip from '@/utils/clipboard'
  import { knowledge_columns } from '@/utils/permission'
  import wechatPush from '@/views/courses/components/wechatPush.vue'
  export default {
    components: {
      addExitCourse: require('./addExitCourse.vue').default,
      wechatPush
    },
    data() {
      return {
        defultImg,
        permission: 3,
        listQuery: {
          columnId: 0,
          selectValue: null,
          searchName: null,
          currentPage: 1, // 当前页
          total: null, // 数据总条数
          limit: 10,
          page: 1
        },
        isShowAdd: false,
        columnData: {}, // 课程专栏点详情过来的专栏信息
        tableCourseData: [], // 课程列表
        isShowExitCourses: false, // 是否显示添加已有课程,
        listLoading: false,
        isShowWechatPush: false,
        courseData: []
      }
    },
    created() {
      this.getModelPermission()
      this.listQuery.columnId = this.$route.params.ColumnId
      this.getColumnInfo()
      this.getCourseList()
    },
    watch: {
      // 监听路由名称变化 重新调用数据
      $route: function(to, from) {
        if (to.name == 'columnDetail') {
          this.listQuery.columnId = this.$route.params.ColumnId
          this.getColumnInfo()
          this.getCourseList()
        }
      }
    },
    directives: {
      focus(el, { value }, { context }) {
        if (value) {
          context.$nextTick(() => {
            el.focus()
          })
        }
      }
    },
    methods: {
      hideWechatPush(msg) {
        this.isShowWechatPush = msg
      },
      messagePush(index) {
        this.isShowWechatPush = true
        this.courseData = this.tableCourseData[index]
      },
      getModelPermission() {
        this.permission = knowledge_columns()
      },
      editSort(index) {
        //    this.isEdit = true;
        this.tableCourseData[index].isEdit = true
      },
      saveSort(index, courseId, sort) {
        if (sort >= 0) {
          var data = { courseId: courseId, sort: sort }
          toSaveSort(data)
            .then(res => {
              if (res.data.data.status) {
                this.$message({
                  type: 'info',
                  message: '保存成功'
                })
                this.tableCourseData[index].isEdit = false
              } else {
                this.$message({
                  type: 'info',
                  message: '保存失败'
                })
              }
            })
            .catch(err => {
              console.log(err)
            })

          return false
        } else {
          this.$message({
            type: 'error',
            message: '排序值必须大于0'
          })
        }
        this.tableCourseData[index].isEdit = false
      },
      getColumnInfo() {
        this.listLoading = true
        toGetColumnInfo(this.$route.params.ColumnId)
          .then(res => {
            // 正确回调
            this.columnData = res.data.data
            this.listLoading = false
          })
          .catch(err => {
            console.log(err)
          })
      },
      getCourseList() {
        this.listLoading = true
        togetCourseList(this.listQuery)
          .then(res => {
            // 正确回调
            for (var i = 0; i < res.data.data.data.length; i++) {
              res.data.data.data[i].isEdit = false
            }
            this.tableCourseData = res.data.data.data
            this.listQuery.total = res.data.data.total
            this.listLoading = false
          })
          .catch(err => {
            console.log(err)
          })
      },
      handle(num) {
        var data = { columnId: this.columnData.columnId, type: num }

        if (num == 5) {
          this.$confirm('关闭内容展示，前端不展示课程，是否需要关闭', '提示', {
            confirmButtonText: '是',
            cancelButtonText: '否',
            type: 'warning'
          }).then(() => {
            this.listLoading = true
            toHandle(data)
              .then(res => {
                // 正确回调
                this.getColumnInfo()
                this.listLoading = false
              })
              .catch(err => {
                console.log(err)
              })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消'
            })
          })
        } else {
          this.listLoading = true
          toHandle(data)
            .then(res => {
              // 正确回调
              this.getColumnInfo()
              this.listLoading = false
            })
            .catch(err => {
              console.log(err)
            })
        }
      },
      hideAddCourses(msg) {
        // 隐藏添加已有课程
        this.isShowExitCourses = msg
        this.getCourseList()
      },
      linkToEdit() {
        this.$router.push({ path: `/columns/edit/${this.$route.params.ColumnId}` })
      },
      linkToEditCourse(courseType, courseId) {
        this.$router.push({
          path: `/courses/${courseType}/${courseId}/edit/${this.$route.params.ColumnId}`
        })
      },
      linkToCreatecourse(type) {
        this.$router.push({ path: `/courses/${type}/create`, query: { columnId: this.$route.params.ColumnId }})
      },
      openFree(courseId, num) {
        var tip
        if (num == 1) {
          tip = '关闭免费'
        } else if (num == 2) {
          tip = '开启免费'
        } else if (num == 3) {
          tip = '移除课程'
        }
        this.$confirm('确定' + tip + '?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var data = { courseId: courseId, type: num }
          toOpenFree(data)
            .then(res => {
              // 正确回调
              this.$message({
                type: 'success',
                message: tip + '成功!'
              })
              this.getCourseList()
            })
            .catch(err => {
              console.log(err)
            })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      },
      handleSizeChange(val) { // 切换每页几条数据方法
        this.listQuery.limit = val
        this.getCourseList()
      },
      handleCurrentChange(val) { // 切换当前页
        this.listQuery.page = val
        this.getCourseList()
      },
      toSerach() {
        this.getCourseList()
      },
      linkToDetail(id, type) {
        if (type == 1) {
          this.$router.push({ name: 'uploadVideo', params: { courseId: id }})
        } else if (type == 2) {
          this.$router.push({ name: 'uploadAudio', params: { courseId: id }})
        }
      },
      addCourses() {
        this.isShowExitCourses = true
      },
      handCopy(url, event) {
        clip(url, event)
      },
      setDefultImg(e) {
        e.target.src = defultImg
      }
    },
    mounted() {
      for (var item in this.tableCourseData) {
        if (this.tableCourseData[item].id === Number(this.$route.params.courseColumnId)) {
          this.columnData = this.tableCourseData[item]
        }
      }
      this.listQuery.total = this.tableCourseData.length
    }
  }
</script>
<style scoped>
  h3 {
    background: #eee;
    border-bottom: 1px solid #eee;
    padding-left: 20px;
    font-weight: 500;
    font-size: 12px;
    line-height: 30px;
    margin: 0;
  }

  a {
    color: #409EFF;
    padding: 0 10px;
  }

  .textRight p {
    text-align: right;
  }

  .colorOrange {
    color: orange;
  }

  .type {
    padding: 0 5px;
    border: 1px solid #409EFF;
    border-radius: 5px;
  }

  .tdedit {
    display: inline;
    background: url(../../../assets/tdedit.gif) no-repeat no-repeat right 0;
    padding-right: 14px;
  }

  .editInput {
    height: 22px;
    width: 40px;
  }
</style>
